package com.hth.service;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hth.data.DataObject;
import com.hth.main.Main;

public class ClientThread extends Thread {
	
	public static final int 	TYPE_INSERT = 1;
	public static final int 	TYPE_QUERY = 2;
	
	private OutputStream 		outputStream;
	private InputStream 		inputStream;
	private DataOutputStream 	dataOutputStream;
	private DataInputStream 	dataInputStream;
	private Socket 				connection;

	public ClientThread(Socket socket) throws IOException {
		this.connection = socket;
		this.outputStream = connection.getOutputStream();
		this.inputStream = connection.getInputStream();
		this.dataOutputStream = new DataOutputStream(outputStream);
		this.dataInputStream = new DataInputStream(inputStream);
	}

	public void run() {
		try {
			String dataFromClient = readDataBuffer();
			displayMessage("Data from client: " + dataFromClient);
			
			String dataSendToClient = processData(dataFromClient);
			sendDataBuffer(dataSendToClient);
		} catch (Exception e) {
			displayMessage("ERROR ClientThread!!!");
		} finally {
			closeConnection();
		}
	}

	private void displayMessage(final String string) {
		System.out.println(string);
	}

	private void sendDataBuffer(String message) {
		PrintWriter printWriter = new PrintWriter(outputStream, true);
		printWriter.println(message);
	}
	
	private String readDataBuffer() throws IOException {
		String dataFromClient = null;
		BufferedReader bufferedReader = 
				new BufferedReader(new InputStreamReader(inputStream));
		dataFromClient = bufferedReader.readLine();
		return dataFromClient;
	}
	
	private String processData(String data) {
		switch (getTypeData(data)) {
			case TYPE_INSERT:
				return insertData(data);
			case TYPE_QUERY:
				return getData(data);
			default:
				return "Toi khong hieu";
		}
	}
	
	private int getTypeData(String data) {
		String arrayString[] = data.split("-");
		if (arrayString[0].equals("insert")) {
			return TYPE_INSERT;
		} else {
			return TYPE_QUERY;
		}
	}
	
	private String getData(String id) {
		String resultString = "Khong thay du lieu!!!";
		try {
			String sql = "SELECT * FROM localtable WHERE id=\'" + id + "\'";
			ResultSet resultSet = Main.MYSQL_MANAGER.executeQuery(sql);
			while (resultSet.next()) {
				String idResult = resultSet.getString("id");
				double xPosition = resultSet.getInt("x_position");
				double yPosition = resultSet.getInt("y_position");
				resultString = idResult + " - " + xPosition + " - " + yPosition;
			}
			resultSet.close();
		} catch (SQLException se) {
			se.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return resultString;
	}
	
	private String insertData(String data) {
		DataObject dataObject = new DataObject(data);
		String sql = "INSERT INTO localtable (id, x_position, y_position) " 
				+ "VALUES ('" + dataObject.getId() + "', " 
				+ dataObject.getxPosition() + ", " + dataObject.getyPosition() + ")";
		Main.MYSQL_MANAGER.executeUpdate(sql);
		return "Server has successfully insert data with id = " + dataObject.getId();
	}
	
	private void closeConnection() {
		try {
			dataOutputStream.close();
			dataInputStream.close();
			connection.close();
		} catch (IOException ioException) {
			displayMessage("ERROR closeConnection!");
			ioException.printStackTrace();
		}
		displayMessage("Closed the connection!");
	}
}